package cn.com.inossem.operation;

import android.content.Context;
import android.os.Bundle;
import cn.com.inossem.network.NetworkConnect;

import com.foxykeep.datadroid.exception.ConnectionException;
import com.foxykeep.datadroid.exception.CustomRequestException;
import com.foxykeep.datadroid.exception.DataException;
import com.foxykeep.datadroid.requestmanager.Request;
import com.foxykeep.datadroid.service.RequestService.Operation;

/**
 * 登录接口
 * 
 * @author wuyt3
 */
public class MessageListOperation implements Operation {
	public static final String MESSAGE_OPERATION_CELLID = "message_operation_cellid";
	public static final String MESSAGE_OPERATION_PAGE = "message_operation_page";
	private static final String TAG = "MessageListOperation";

	@Override
	public Bundle execute(Context context, Request request)
			throws ConnectionException, DataException, CustomRequestException {
		String cellid = request.getString(MESSAGE_OPERATION_CELLID);
		String id = request.getString(MESSAGE_OPERATION_PAGE);
		NetworkConnect connection = new NetworkConnect(
				"http://192.168.0.102:8082/internet-admin/rest/login/userInfo");
		connection
				.setPostText("{\"username\":\""
						+ "admin" + "\",\"password\":\"" + "admin" + "\"}");
		System.out
				.println("{\"username\":\""
						+ "admin" + "\",\"password\":\"" + "admin" + "\"}");
		
		String body = "";
		
		body = connection.execute();
		System.out.println("MessageListOperation  response" + body);
		
//		NetworkConnect connection1 = new NetworkConnect(
//				"http://192.168.0.102:8082/internet-admin/rest/projectInfo/queryProjectInfo");
//		connection1
//				.setPostText("{\"pageNum\":\""
//						+ "1" + "\",\"pageSize\":\"" + "10" + "\"}");
//		System.out
//				.println("{\"pageNum\":\""
//						+ "1" + "\",\"pageSize\":\"" + "10" + "\"}");
//		
//		String body1 = "";
//		
//		
//		body1 = connection1.execute();
//		System.out.println("jobs  response" + body1);
		
//		String villageid = Utils.getVillageID(context);
//		NetworkInfo network = NetWorkUtil.getNetworkInfo(context);
//		DBHelper dbHelper = new DBHelper(context);
//		try {
//			dbHelper.createDataBase();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		SQLiteDatabase db = dbHelper.getWritableDatabase();
//		if (network != null) {
//			System.out.println("有网络！");
//		body = connection.execute();
//		System.out.println("MessageListOperation  response" + body);
//			// 如果第一页存缓存
//			if (id.equals("")) {//是否是第一页
//				System.out.println("是第一页加载");
//				
//				boolean isexist = getNoticeCache(db, villageid);
//				if (isexist) {
//					// 如果存在修改缓存
//					updateNotice(db, villageid, body);
//				} else {
//					// 如果不存在新增缓存
//					insertNotice(db, villageid, body);
//				}
//
//				getNoticeCache(db, villageid);
//			}
//		} else {
//			System.out.println("无网络！");
//			body = getNotice(db, villageid);
//		}
		Bundle bundle = new Bundle();
//		try {
//			JSONObject jsonObject = new JSONObject(body);
//			if (jsonObject.has("head")) {
//				List<Messages> retList;
//				String head = jsonObject.getString("head");
//				Gson gson = new Gson();
//				CommData data = gson.fromJson(head, CommData.class);
//				int code = data.getResultcode();
//				if (code == WSConfig.RESULT_SUCESS) // 成功
//				{
//					// {"body":{"data":{},"list":[{"id":10000050,"cellid":1001,"cellname":"上地佳园小区","noticename":"werwrwr","content":"wqrewqr","starttime":"jun 10, 2014 12:00:00 am","endtime":"jun 25, 2014 12:00:00 am","flag":1,"remark":"qrqrqr"},{"id":1,"cellid":1001,"cellname":"上地佳园小区","noticename":"停电","content":"因线路检修，5月30日停电一天！","starttime":"may 30, 2014 12:00:00 am","endtime":"jun 1, 2014 12:00:00 am","flag":1,"remark":"utyuty"}]},"head":{"action":"notice","resultcode":"0","errormsg":"ok!"}}
//
//					String resultbody = jsonObject.getString("body");
//					JSONObject bodyObject = new JSONObject(resultbody);
//
//					String resultlist = bodyObject.getString("list");
//					// json转为带泛型的list
//					retList = gson.fromJson(resultlist,
//							new TypeToken<List<Messages>>() {
//							}.getType());
//
//					// 返回巡检记录列表
//					bundle.putParcelableArrayList(
//							WooRequestFactory.RESPONSE_PATRO_LIST,
//							(ArrayList<? extends Parcelable>) retList);
//				}else{
//					retList = new ArrayList<Messages>();
//				}
//				// 设置登陆结果
//				bundle.putInt(WooRequestFactory.BUNDLE_EXTRA_ORDER_LIST, code);
//			}
//		} catch (JSONException e) {
//			e.printStackTrace();
//		}
//		db.close();
		return bundle;
	}
	
//	//查询指定小区缓存
//	private String getNotice(SQLiteDatabase db, String VillageID) {
//		Cursor c = db.rawQuery("SELECT * from notice" + " where id=?",
//				new String[] { VillageID });
//		if (c.moveToFirst()) {
//			String content = c.getString(c.getColumnIndex("content"));
//			return content;
//		}
//
//		return "";
//	}
//	// 查询公告缓存是否存在
//	private boolean getNoticeCache(SQLiteDatabase db, String VillageID) {
//		Cursor c = db.rawQuery("SELECT * from notice" + " where id=?",
//				new String[] { VillageID });
//		if (c.moveToFirst()) {
//			// int personid=cursor.getInt(cursor.getColumnIndex("personid"));
//			// String name=cursor.getString(cursor.getColumnIndex("name"));
//			String content = c.getString(c.getColumnIndex("content"));
//			System.out.println("显示插入没有" + content);
//			return true;
//		}
//
//		return false;
//	}
//
//	// 修改公告缓存
//	private void insertNotice(SQLiteDatabase db, String VillageID, String body) {
//		db.execSQL("INSERT INTO notice(id,content) values(?,?)", new Object[] {
//				VillageID, body });
//	}
//
//	// 添加公告缓存
//	private void updateNotice(SQLiteDatabase db, String VillageID, String body) {
//		db.execSQL("update notice set content=? where id=?", new Object[] {
//				body, VillageID });
//
//	}
}
